import {Component} from '@angular/core';
import {
    AlertController,
    LoadingController,
    NavController,
    NavParams,
    Platform,
    ToastController
} from 'ionic-angular';
import {BasePage} from "../../BasePage";
import {SystemService} from "../../service/SystemService";
import {UserManager} from "../../../framework/manager/UserManager";
import {Log} from "../../../framework/utils/Log";
import {ExamineApprovePage} from "../../examine/examineApprove";
import {ApplyStoreSLS} from "../../apply/applyStoreSLS";

/**
 * Generated class for the MyExpensesPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
    selector: 'page-my-expenses',
    templateUrl: 'my-expenses.html',
})
export class MyExpensesPage extends BasePage {

    private static TAG: string = 'MyExpensesPage';
    context: string;
    private _unitId: string;
    private _userId: string;
    private _pageRow: number = 1;
    private _rowNum: number = Math.ceil(window.innerHeight / 60);
    private isEnd: boolean = false;
    expensesList: any = [];

    constructor(public navCtrl: NavController, public navParams: NavParams, public sys: SystemService, public platform: Platform,
                public loadingCtrl: LoadingController,
                public toastCtrl: ToastController,
                public alertCtrl: AlertController) {
        super(platform, loadingCtrl, toastCtrl, alertCtrl);
        this._unitId = UserManager.getInstance().user.unitId;
        this._userId = UserManager.getInstance().user.userId;

    }

    ionViewDidLoad() {
        Log.w(MyExpensesPage.TAG, 'ionViewDidLoad MyExpensesPage')
        console.log('ionViewDidLoad MyExpensesPage');
        this.getExpensesInfo();
    }
    ionViewDidEnter(){
    }
    getExpensesInfo() {
        this.showLoading(true, '请稍后');
        let expensesInfo: any = {
            DanWeiBH: this._unitId,
            YongHuBH: this._userId,
            PageRow: this._pageRow,
            RowNum: this._rowNum
        };
        this.sys.getExpensesList(expensesInfo).subscribe((res: any) => {
            if (res.msgCode == '1') {
                this.expensesList = res.list;
            } else {
                this.isEnd = true;
            }
            this.showLoading(false, '请稍后');
        }, error => {
            this.showLoading(false, '请稍后');
            this.showError('数据请求失败')
        });
    }

    notifyHandler(item) {

        this.navCtrl.push(ExamineApprovePage, {data: item, type: '2'})
    }

    doInfinite(infiniteScroll) {
        console.log('Begin infiniteScroll async operation');
        this.showLoading(true, '请稍后');
        setTimeout(() => {
            this._pageRow++;
            let expensesInfo: any = {
                DanWeiBH: this._unitId,
                YongHuBH: this._userId,
                PageRow: this._pageRow,
                RowNum: this._rowNum
            };
            this.sys.getExpensesList(expensesInfo).subscribe((res: any) => {
                if (res.msgCode == '1' && res.list && res.list.length > 0) {
                    this.expensesList.push(...res.list);
                } else {
                    this.isEnd = true;
                }
                this.showLoading(false, '请稍后');
            }, error => {
                this.showLoading(false, '请稍后');
                this.showError('数据请求失败')
            });
            console.log('Async infiniteScroll operation has ended');
            infiniteScroll.complete();
            if (this.isEnd) {
                infiniteScroll.enable(false);
                this.showLoading(false, '请稍后');
                //this.showError("已加载所有");
            }
        }, 500);
    }

    doRefresh(refresher) {
        console.log('Begin refresher async operation');
        setTimeout(() => {
            this._pageRow = 1;
            this.getExpensesInfo();
            console.log('Async refresher operation has ended');
            refresher.complete();
        }, 2000);
    }

}
